package burubaka.bat;

import java.util.List;
import java.util.logging.Logger;

import burubaka.BurubakaConstants;
import burubaka.model.PayOff;
import burubaka.model.RaceInfo;
import burubaka.model.ResultHorse;
import burubaka.service.PayOffService;
import burubaka.service.RaceInfoService;
import burubaka.service.ResultHorseService;

public class RaceResultBat {

	private static final Logger log = Logger.getLogger(RaceResultBat.class.getName());

	private ResultHorseService resultHorseService = new ResultHorseService();
	private PayOffService payOffService = new PayOffService();
	private RaceInfoService raceInfoService = new RaceInfoService();

	public RaceInfo execute() throws Exception {

		// レース情報
		RaceInfo raceInfo = raceInfoService.getCurrentRaceInfo();

		// すでにレース情報が登録されているかどうか判断
		// 既にレース結果が登録されてる場合も何もしない
		if (raceInfo == null || raceInfo.getProgress() >= BurubakaConstants.PROGRESS_RACERESULT) {
			return raceInfo;
		}

		List<ResultHorse> rhList = resultHorseService.getResultHorseListByOuter(raceInfo);

		if (rhList.size() == 0) {
			return raceInfo;
		}

		// レース結果登録
		resultHorseService.regist(rhList, raceInfo);

		// 配当登録
		PayOff payOff = payOffService.getPayOff(raceInfo);
		payOffService.regist(payOff, raceInfo.getKey());

		// レース情報更新
		raceInfo.setProgress(BurubakaConstants.PROGRESS_RACERESULT);
		raceInfoService.regist(raceInfo);

		return raceInfo;
	}
}
